﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.Framework;
using WebAppAuthAdmin.Code;
using WebAppAuthAdmin.Code.Security;

namespace WebAppAuthAdmin.App
{
    public partial class CambiarPassword : BasePage
    {
        private string UserName
        {
            get
            {
                return Convert.ToString(ViewState["UserName"]);
            }
            set
            {
                ViewState["UserName"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {               
                if (!this.IsPostBack)
                {
                    UserName = this.Request.QueryString["UserName"];
                    if (String.IsNullOrEmpty(UserName))
                    {
                        UserName = BaseWebPage.UserName;
                    }

                    this.lblUserName.Text = UserName;

                    bool puedeCambiarPassword = false;
                    if (BaseWebPage.UserName.ToLower() != UserName)
                    {
                        puedeCambiarPassword = HasPermissionFor(AppSecurity.APP_CHANGE_PASSWORD);
                    }
                    else
                    {
                        puedeCambiarPassword = true;
                    }

                    if (!puedeCambiarPassword)
                    {
                        pnlCambioPass.Visible = false;
                        this.masterPage.ShowMessage(string.Format("No tiene permisos para cambiar el password de {0}", UserName), WebMessageType.Warning);
                    }
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void lnkGuardar_Click(object sender, EventArgs e)
        {
            try
            {
                bool puedeCambiarPassword = false;
                if (BaseWebPage.UserName.ToLower() != UserName)
                {
                    puedeCambiarPassword = HasPermissionFor(AppSecurity.APP_CHANGE_PASSWORD);
                }
                else
                {
                    puedeCambiarPassword = true;
                }

                if (puedeCambiarPassword)
                {
                    string newPassword = this.txtPasswordNuevo.Text;
                    string oldPassword = this.txtPasswordAnterior.Text;

                    if (WebServiceHelper.ChangePassword(UserName, oldPassword, newPassword))
                    {
                        this.masterPage.ShowMessage("El cambio de password fue exitoso", WebMessageType.Notification);
                    }
                    else
                    {
                        this.masterPage.ShowMessage("El cambio de password NO fue exitoso", WebMessageType.Notification);
                    }
                }
                else
                {
                    this.masterPage.ShowMessage(string.Format("No tiene permisos para cambiar el password de {0}", UserName),  WebMessageType.Warning);
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void lnkCancelar_Click(object sender, EventArgs e)
        {
            try
            {
                this.Response.Redirect(string.Format("/App/UserViewApplications.aspx?UserName={0}", UserName), true);
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }
    }
}